package com.itstyle.quartz.web;

import com.itstyle.quartz.entity.Auth;
import com.itstyle.quartz.entity.Menu;
import com.itstyle.quartz.service.impl.AuthService;
import com.itstyle.quartz.service.impl.MenuService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.Principal;
import java.util.List;

/**
 * @author 张霄峰
 * @email 29839415@qq.com
 * @date 2020/4/14 13:24
 */
@Controller
public class LoginController {
    @Autowired
    AuthService userMapper;
    @Autowired
    MenuService menuService;


    /**
     * 登录页面
     *
     * @param
     * @return
     */
    @ApiOperation(value="自定义登陆方法")
    @RequestMapping(value = "/")
    public String login(Model model, Auth auth
            , @RequestParam(value = "error", required = false) boolean error
            , @RequestParam(value = "logout", required = false) boolean logout, HttpServletRequest request, Principal principal) {
        model.addAttribute(auth);
        if (principal != null) {
            model.addAttribute("principal", principal);
        }
        //如果已经登陆跳转到个人首页
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && !authentication.getPrincipal().equals("anonymousUser") &&  authentication.isAuthenticated()) {
            List<Menu> menuLists = menuService.findByUserId();
            model.addAttribute("menuList",menuLists);
            return "main";
        }
        if (error == true)
            model.addAttribute("error", error);
        if (logout == true)
            model.addAttribute("logout", logout);

        return "login";

    }


    @RequestMapping("/logins")
    @ApiOperation(value="登录方法")
    public String login(HttpServletRequest request, Model model, HttpSession session) {

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //查询用户信息
        Auth aut = userMapper.getuser(username, password);

        if (aut != null) {
            System.out.println("登录成功");
            session.setAttribute("user", aut);
            model.addAttribute("user", aut);
            return "main";
        } else {

            System.out.println("登录失败");
            return "login";
        }

    }

    /**
     * 查询用户所有信息
     *
     * @return
     */
    @ApiOperation(value="查询用户所有信息")
    @RequestMapping("/u")
    public List<Auth> getauth() {
        List<Auth> auth = userMapper.finall();
        return auth;
    }


    @RequestMapping("/userInfo")
    @ApiOperation(value="查询当前登录用户信息")
    @ResponseBody
    public User showUsername(HttpServletRequest request){
//通过SecurityContextHolder获得当前线程上绑定的SecurityContext对象
        SecurityContext context = SecurityContextHolder.getContext();
//也可以通过session获得SecurityContext对象
        SecurityContext context_session = (SecurityContext)
                request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
//证明线程绑定的SecurityContext与session域中存储的SecurityContext是同一个
        System.out.println(context==context_session);
//        2、获得用户名方式二：
//        在页面中通过el表达式从session域中获得
//        3、获得用户名方式三：
//        在页面中通过SpringSecurity的标签直接获得
//        4、在header.jsp中显示用户名
//获得Authentication认证对象
        Authentication authentication = context_session.getAuthentication();
//获得User对象
        User user = (User) authentication.getPrincipal();
//获得用户名
        return user;
    }
}
